package it.fantacalcio.struts.form;

import it.fantacalcio.model.User;
import it.fantacalcio.spring.service.MyServiceFactory;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class SquadraForm extends ActionForm {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Long id;
	private String nome;
	private Integer crediti;
	private Long idUser;
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getNome() {
		return nome;
	}
	public void setNome(String nome) {
		this.nome = nome.equals("") ? null : nome;
	}
	public Integer getCrediti() {
		return crediti;
	}
	public void setCrediti(Integer crediti) {
		this.crediti = crediti==0 ? null : crediti;
	}
	public Long getIdUser() {
		return idUser;
	}
	public void setIdUser(Long idUser) {
		this.idUser = idUser;
	}
	
	@Override
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		
		ActionErrors errors = new ActionErrors();
		if(this.getNome()==null || this.getNome().equals("")){
		 	errors.add("nome", new ActionMessage("errors.squadra.nome.stringa_vuota"));
		}
		if(this.getCrediti()==null || this.getCrediti() < 1){
		 	errors.add("crediti", new ActionMessage("errors.squadra.crediti.stringa_vuota"));
		}
		if (this.getIdUser()==null || this.getIdUser()<1){
			errors.add("idUser", new ActionMessage("errors.squadra.user.non_selezionato"));
		}
		List<User> listaUser=new ArrayList<User>();
		if(errors.size()>0){
			try{
				listaUser=MyServiceFactory.getUserService().list();			
			}catch (Exception e) {
				e.printStackTrace();
			}
		}
		request.setAttribute("listaUser", listaUser);
		return errors;
	}

}
